package com.woniuxy.lab.woniu_lab_boss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.lab.woniu_lab_boss.Security.comment.LabAccountDetail;
import com.woniuxy.lab.woniu_lab_boss.controller.form.LoginForm;
import com.woniuxy.lab.woniu_lab_boss.exception.AccountException;
import com.woniuxy.lab.woniu_lab_boss.model.Account;
import com.woniuxy.lab.woniu_lab_boss.dao.AccountDao;
import com.woniuxy.lab.woniu_lab_boss.service.AccountService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lab.woniu_lab_boss.service.dto.AccountDto;
import com.woniuxy.lab.woniu_lab_boss.util.JwtUtil;
import com.woniuxy.lab.woniu_lab_boss.util.WebUtil;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Objects;

import static com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode.ACCOUNT_IS_NOT_EXISTS;
import static com.woniuxy.lab.woniu_lab_boss.exception.WebExceptionCode.LOGIN_FAIL;

/**
 * <p>
 * 账户表 服务实现类
 * </p>
 *
 * @author Silas
 * @since 2023-03-15
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountDao, Account> implements AccountService {

    public static final String LAP_BOSS_TOKEN = "lap-boss-token";
    public static final String ACCOUNT_ID = "accountId";
    public static final String TYPE = "type";
    public static final String PERSON_ID = "person_id";

    @Resource
    private AuthenticationManager authenticationManager;

    @Override
    public String[] login(LoginForm form) {

        UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
                form.getUsername(),form.getPassword());
        SecurityContextHolder.getContext().setAuthentication(usernamePasswordAuthenticationToken);
        //AuthenticationManager authentication进行用户认证
        Authentication authentication = authenticationManager.authenticate(usernamePasswordAuthenticationToken);

        //如果没有认证成功, 给出相应的提示
        if(Objects.isNull(authentication)){
            throw new AccountException(LOGIN_FAIL);
        }

        //认证成功生成jwt
        LabAccountDetail loginUser = (LabAccountDetail)authentication.getPrincipal();
        //得到ACCOUNT_ID,TYPE
        HashMap<Object, Object> map = new HashMap<>(2);
        map.put(ACCOUNT_ID,loginUser.getId());
        map.put(TYPE,loginUser.getType());
        map.put(PERSON_ID, loginUser.getPersonId());
        String token = JwtUtil.getToken(map, 2);
        //将token添加到请求头
        HttpServletResponse response = WebUtil.getResponse();
        response.addHeader(LAP_BOSS_TOKEN,token);
        //返回type给前端
        String[] res = new String[2];
        res[0] = token;
        res[1] = loginUser.getType();
        return res;
    }

    @Override
    public AccountDto findByToken() {

        Integer id = JwtUtil.getUserIdFromReq();
        if(id == null||id == 0){
            throw new AccountException(ACCOUNT_IS_NOT_EXISTS);
        }

        Account account = getById(id);

        return BeanUtil.copyProperties(account, AccountDto.class);
    }


}
